﻿using System;

/// <summary>
/// the string parser who use SPACE to separate string into many parts
/// </summary>
public class CmdParser
{
    private const string SPACE = " ";

    private readonly string m_Spliter;
    private readonly string[] m_ParameterSpliters;

    /// <summary>
    /// Initializes a new instance of the <see cref="BasicStringParser"/> class.
    /// </summary>
    public CmdParser()
        : this(SPACE, SPACE)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="BasicStringParser"/> class.
    /// </summary>
    /// <param name="spliter">The spliter between command name and command parameters.</param>
    /// <param name="parameterSpliter">The parameter spliter.</param>
    public CmdParser(string spliter, string parameterSpliter)
    {
        m_Spliter = spliter;
        m_ParameterSpliters = new string[] { parameterSpliter };
    }

    /// <summary>
    /// parse the source string into key, body and parameters parts
    /// </summary>
    /// <param name="source">the source string</param>
    /// <param name="key">the parsed key</param>
    /// <param name="body">the parsed body</param>
    /// <param name="parameters">the parsed parameter</param>
    public void Parse(string source, out string key, out string body, out string[] parameters)
    {
        if (source == null)
        {
            key = "";
            body = "";
            parameters = null;
            return;
        }

        int pos = source.IndexOf(m_Spliter);

        if (pos > 0)
        {
            key = source.Substring(0, pos);
            body = source.Substring(pos + 1);
            parameters = body.Split(m_ParameterSpliters, StringSplitOptions.RemoveEmptyEntries);
        }
        else
        {
            key = body = source;
            parameters = null;
        }
    }
}

